if (SERVER) then
 	AddCSLuaFile("shared.lua");
end;

if (CLIENT) then
	SWEP.PrintName 		= "Breaching Shotgun";
	SWEP.Instructions	= "Left click to shoot.";

	SWEP.Slot			= 2;
	SWEP.SlotPos		= 1;
	SWEP.ViewModelFlip 	= false;
	SWEP.DrawAmmo 		= false;
	SWEP.ViewModelFOV	= 62;
end;

SWEP.Base = "leaf_weapon_base";

SWEP.Spawnable		= true;
--SWEP.AdminSpawnable		= true;

SWEP.ViewModel			= Model("models/weapons/v_shotgun.mdl");
SWEP.WorldModel			= Model("models/weapons/w_shotgun.mdl");

SWEP.Primary.Sound		= Sound("Weapon_Shotgun.Single");

SWEP.Primary.Recoil			= 0.5;
SWEP.Primary.Cone			= 0.1;
SWEP.Primary.Damage			= 4;
SWEP.Primary.NumShots		= 9;

SWEP.Primary.ClipSize		= 6;
SWEP.Primary.DefaultClip 	= 6;
SWEP.Primary.Ammo 			= "buckshot";
SWEP.Primary.Delay 			= 1;
SWEP.Primary.Automatic 		= false;

SWEP.IronSightPos = Vector(-9, -5, 3);
SWEP.IronSightAng = Vector(-.5, -1, 0);

SWEP.HolsteredPos = Vector(10.8, -5, -2);
SWEP.HolsteredAng = Vector(-5.0, 50, 0);

SWEP.HoldType = "shotgun";
 
SWEP.Reloading = false;

function SWEP:Reload()
 	if (self.Reloading) then return; end;
	
	if (self.Weapon:Clip1() < self.Primary.ClipSize and self.Owner:GetAmmoCount(self.Primary.Ammo) > 0) then
		self.Weapon:SetVar("reloadtimer", CurTime() +0.5);
		self.Weapon:SendWeaponAnim(ACT_SHOTGUN_RELOAD_START);
		self.Owner:SetAnimation(PLAYER_RELOAD);
		
		if (SERVER) then
			self.Owner:EmitSound("weapons/shotgun/shotgun_reload" .. math.random(1, 3) .. ".wav");
		end;
		
		self.Reloading = true;
	end;
end;

function SWEP:Think()	
	if (self.Reloading) then
		if (self.Weapon:GetVar("reloadtimer") < CurTime()) then 
			if (self.Weapon:Clip1() >= self.Primary.ClipSize) then
				self.Weapon:SendWeaponAnim(ACT_SHOTGUN_RELOAD_FINISH);
				
				if (SERVER) then
					self.Owner:EmitSound("weapons/shotgun/shotgun_cock.wav");
				end;
			
				self.Reloading = false;
				return;
			end;

			self.Weapon:SetVar("reloadtimer", CurTime() +0.5);
			self.Weapon:SendWeaponAnim(ACT_VM_RELOAD);
			
			if (SERVER) then
				self.Owner:EmitSound("weapons/shotgun/shotgun_reload" .. math.random(1, 3) .. ".wav");
			end;
			
			self.Owner:RemoveAmmo(1, self.Primary.Ammo, false);
			self.Weapon:SetClip1(self.Weapon:Clip1() +1);
		end;
 	end;
end;

function SWEP:PrimaryAttack()
	self.Weapon:SetNextSecondaryFire(CurTime() +self.Primary.Delay);
	self.Weapon:SetNextPrimaryFire(CurTime() +self.Primary.Delay);
	
	if (!self.Owner:GetNWBool("leaf_holstered") or !self:CanPrimaryAttack()) then return; end;
	
	self.Reloading = false;
	
	if (SERVER) then
		-- Play shoot sound.
		local primarySound = self.Primary.Sound;
		local soundType = type(primarySound);
		
		if (soundType == "table") then
			self.Owner:EmitSound(primarySound[math.random(1, #primarySound)]);
		else
			self.Owner:EmitSound(primarySound);
		end;
	end;
	
	-- Shoot the bullet.
	self:CSShootBullet(self.Primary.Damage, self.Primary.Recoil, self.Primary.NumShots, self.Primary.Cone);
	
	if (SERVER) then
		local tr = self.Owner:EyeTrace(120);
		
		if (IsValid(tr.Entity) and tr.Entity:IsDoor()) then
			local normal = (tr.Entity:GetPos() -self.Owner:GetPos()):Normalize();
			local push = 10000 *normal;
			
			local class, position, skin, material = tr.Entity:GetClass(), tr.Entity:GetPos(), tr.Entity:GetSkin(), tr.Entity:GetMaterial();
			local angles, model, data = tr.Entity:GetAngles(), tr.Entity:GetModel(), SCHEMA:GetDoorData(tr.Entity:EntIndex());
			
			local validBreach = util.IsValidModel(model);
			
			if (tr.Entity.combineSpecial) then
				tr.Entity:Fire("unlock", "", 0);
				tr.Entity:Fire("toggle", "", 0);
			elseif (class == "func_movelinear") then
				tr.Entity:Fire("unlock", "", 0);
				tr.Entity:Fire("open", "", 0);
			elseif (class == "func_door_rotating" and !validBreach) then
				tr.Entity:Fire("unlock", "", 0);
				tr.Entity:Fire("open", "", 0);
			else
				if (validBreach) then
					tr.Entity:SetNotSolid(true);
					tr.Entity:SetNoDraw(true);
					tr.Entity:Fire("open", "", 0);
					tr.Entity:Fire("lock", "", 0.3);
		
					local door = ents.Create("prop_physics");
					door:SetModel(model);
					door:SetSkin(skin);
					door:SetMaterial(material);
					door:SetAngles(angles);
					door:SetPos(position);
					door:Spawn();
			
					local physics = door:GetPhysicsObject();
				
					if (IsValid(physics)) then -- Should always be valid.
						door:GetPhysicsObject():ApplyForceOffset(push, tr.HitPos);
					else
						door:SetVelocity(push);
					end;
		
					timer.Simple(40, function()
						tr.Entity:SetNotSolid(false);
						tr.Entity:SetNoDraw(false);
						tr.Entity:Fire("unlock", "", 0);
					end);
			
					timer.Simple(25, function()
						if (IsValid(door)) then
							door:Remove();
						end;
					end, door);
				end;
			end;
		end;
	end;
	
	-- Remove 1 bullet from our clip.
	self:TakePrimaryAmmo(1);
	
	if (self.Owner:IsNPC()) then return; end;
	
	-- Punch the player's view.
	self.Owner:ViewPunch(Angle(math.Rand(-2, 2) *self.Primary.Recoil, math.Rand(-1.5, 1.5) *self.Primary.Recoil, 0));
end;